/**
 * 用户相关状态
 */
 import { 
  setUserToken,
  getUserToken,
  getUserRefreshToken
} from "@/utils/auth";
import api from '@/api';
export default {
  namespaced: true,

  state:{
    toKen:getUserToken(),
    refresh_token:getUserRefreshToken(),
  },

  mutations: {
    SET_TOKEN: (state, info) => {
      state.toKen = info
    },
    SET_REFRESHTOKEN: (state, info) => {
      state.refresh_token = info
    },
  },

  actions: {
    // 登录
    Login({ commit }, usr_token) {
      console.log(usr_token)
      return new Promise((resolve, reject) => {
        api.user.getLogin({usr_token}).then(res=>{
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登录-验证手机号
    LoginPhone({ commit }, info) {
      console.log(info)
      let {code,phone} = info
      let data = {
        code,
        phone
      }
      return new Promise((resolve, reject) => {
        api.user.verificationLogin(data).then(res=>{
          if(res.code == 200){
            if(!res.data.status){
              setUserToken(res.data.token)
              commit('SET_TOKEN', res.data.token)
              commit('SET_USER', res.data.user)
            }
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
  },

  getters: {
  },
};
